package _0977_Squares_of_a_Sorted_Array;

import java.util.Arrays;

/**
 * Date: 2020/10/16 10:52
 * Content:
 * 执行用时：1 ms, 在所有 Java 提交中击败了100.00%的用户
 * 内存消耗：40.2 MB, 在所有 Java 提交中击败了62.02%的用户
 */
public class Solution {

    public int[] sortedSquares(int[] nums) {

        int n = nums.length;

        int[] res = new int[n];
        int i = 0, j = n - 1, k = n - 1;
        while (i <= j) {
            int t;
            if (Math.abs(nums[i]) > Math.abs(nums[j])) t = nums[i++];
            else t = nums[j--];

            res[k--] = t * t;
        }

        return res;
    }

    public static void main(String[] args) {

        int[] A = {-4, -1, 0, 3, 10};
        System.out.println(Arrays.toString((new Solution()).sortedSquares(A)));
    }
}
